<template>
  <div class="app-container" :style="setBgColor(albumpic_big)">
    <div class="left-container">
      <div class="equipment">
        <x-tabs
          title="选择设备"
          :list="equipmentList"
          label="equipmentName"
          :shadow="false"
          :required="true"
          :width="200"
          align="center"
          styleLess="block"
          @tab-click="changeEquipmen" />
      </div>
      <div class="answer-sheet">
        <div class="question-type">
          <div>
            <div v-for="(list, name) in questionType" :key="name">
              <div class="type-title">{{ name }}型题</div>
              <el-tag
                v-for="(num, index) in list.showdata"
                :key="index"
                :effect="
                  currentQuestionMark.type === name && currentQuestionMark.currentQuestionIndex === index
                    ? 'dark'
                    : 'light'
                "
                @click="changeQuestionMark(name, index, list.id)">
                {{ num }}
              </el-tag>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="right-container">
      <div v-if="questionList.length > 0" :infinite-scroll-disabled="loading" class="question-list">
        <div v-for="(questionItem, index) in updateQuestionList" :key="questionItem.createTime" class="question">
          <question :question-info="questionItem" :question-type="questionType" />
        </div>
      </div>
      <div v-else class="noMore">没有更多了</div>

      <el-pagination background layout="prev, pager, next" @current-change="changePage" :total="total"> </el-pagination>
    </div>
    <!-- question-list -->
    <!-- loading -->
    <!-- <div
      v-show="loading"
      v-loading="true"
      element-loading-text="加载中"
      element-loading-spinner="el-icon-loading"
      style="height: 60px"
    /> -->
  </div>
</template>
<script>
import Question from './Question.vue'
import XTabs from '@/components/XTabs.vue'
import selectQuestion from './selectQuestion.vue'
import { getEquipment } from '@/api/report'
import { findWrongQuestionType, findWrongQuestion } from '@/api/wrongQuestion'
export default {
  name: 'WrongQestion',
  components: { Question, XTabs, selectQuestion },
  data() {
    return {
      // currentIndex: 0,
      total: 0,
      pageNum: 1,
      pageSize: 10,
      questionList: [],
      loading: false,
      noMore: false,
      equipmentList: [],
      equipment: '',
      albumpic_big: '',
      readOnly: false,
      question: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      // 当前被点击的题目
      currentQuestionMark: {},
      // 获取题目类型以及每个类型对应的题目
      questionType: {},
      // 默认的题目类型
      quType: '',
      topImg: {
        // 'PET/CT': require('../../assets/shebei_images/PETCT.jpg'),
        // 'SPECT/CT': require('../../assets/shebei_images/SPECT.jpg'),
        // 'PET/MR': require('../../assets/shebei_images/PETMR.jpg'),
        // 'BMD': require('../../assets/shebei_images/BMD.jpg'),
      },
    }
  },
  async created() {
    console.log('created')
    await this.getEquipmentData()
    // 获取题目类型中的第一个
    console.log(this.equipmentList[0].id, 'equipmentList')
    // 获取初始化的时候的题目类型
    await this.getQuestionType(this.equipmentList[0].id)
    const type = Object.keys(this.questionType)[0]
    console.log(type, 'type')
    this.$set(this.currentQuestionMark, 'type', type)
    // 获取初始化的时候的题目id
    this.$set(this.currentQuestionMark, 'currentQuestionIndex', 0)
  },
  mounted() {
    console.log('mounted')
  },
  computed: {
    updateQuestionList() {
      return this.questionList
    },
  },
  methods: {
    // 改变页码的时候调用
    changePage(page) {
      this.pageNum = page
      this.getWrongQuestionList()
      console.log(page, 'page')
    },
    setBgColor(bgColor) {
      if (bgColor === '' || bgColor === '' || bgColor === undefined) {
        return { background: 'url(' + bgColor + ') center center no-repeat' }
      } else {
        return { background: 'url(' + bgColor + ') center center no-repeat' }
      }
    },
    // 更改答题卡中选择的题目
    changeQuestionMark(name, index, id) {
      console.log(name, 'changeName')
      // console.log(this.questionType, 'questionType')
      // console.log(item, 'currentQuestionMark')
      console.log(index, 'QuestionIndex')
      this.$set(this.currentQuestionMark, 'currentQuestionIndex', index)
      this.$set(this.currentQuestionMark, 'type', name)
      // console.log(this.currentQuestionMark, 'currentQuestionMark')
      this.quType = id
      // console.log(transformData(item), 'transformData')
      this.getWrongQuestionList()
      // console.log(item, index, 'item', 'index')
      const questionContainer = document.querySelector('.question-list')
      const question = document.querySelector('.question')
      const questionHeight = question.clientHeight // 单个题目的高度
      const scrollDistance = index * (questionHeight + 14) // 滚动的距离
      // 设置题目容器的滚动距离，实现滚动
      questionContainer.scrollTop = scrollDistance
    },
    // // 滚动到底部的时候会再次请求加载
    // load() {
    //   this.getWrongQuestionList()
    // },
    // 获取题目类型数据
    async getQuestionType(id) {
      // 获取题目类型的id
      console.log(this.equipment, 'equipment')
      const res = await findWrongQuestionType({
        questionEquipmentId: id ?? this.equipment.item.id,
      })
      console.log(res.data, '获取题目分类')
      // 判断是否有题目
      if (Object.keys(res.data).length === 0) {
        this.questionType = {}
        this.questionList = []
        return
      }
      // 对数据进行简单处理
      for (const key in res.data) {
        console.log(key, 'key')
        if (key > 10) res.data[key].showdata = 10
        else res.data[key].showdata = +key
      }
      // 设置一个对象用来存储题目类型以及id
      let questionType = {}
      Object.entries(res.data).forEach((item) => {
        questionType[item[1].name] = {
          id: item[1].id,
          showdata: item[1].showdata,
        }
      })
      this.questionType = questionType
      const type = Object.keys(this.questionType)[0]
      this.quType = this.questionType[type].id
      await this.getWrongQuestionList()
      // http://localhost:8080/student/findWrongOneType?questionEquipmentId=2？quType=id
    },

    async getWrongQuestionList() {
      console.log('getWrongQuestionList')
      console.log(this.quType, 'quType')
      // console.log(this.equipment.item.id)
      const res = await findWrongQuestion({
        questionEquipmentId: this.equipment.item.id,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        quType: this.quType,
      })
      console.log(res, 'res.data')
      if (this.questionList.length > 0) {
        this.questionList = []
        this.questionList.push(...res.rows)
      } else {
        this.questionList.push(...res.rows)
      }

      this.total = res.total
    },
    //  更改选择设备的时候调用
    async changeEquipmen(e) {
      console.log('changeEquipmen')
      //选择设备标题
      this.equipment = e
      // console.log(this.equipment.item.id);
      // this.questionList = []
      console.log('当前点击的设备', e)
      // 初始化的时候当前页码为1
      this.noMore = false
      await this.getQuestionType()
      let str = e.item.equipmentName
      console.log(str, 'str')
      this.albumpic_big = this.topImg[str] //undefined
      // console.log(this.albumpic_big, 'albumpic_big')
    },

    async getEquipmentData() {
      // 在页面被渲染之前就获取数据
      //获取设备数据
      let res = await getEquipment()
      console.log('获取设备数据', res)
      if (res.code == 200) {
        this.equipmentList = res.equipment.map((item) => {
          return { equipmentName: item.equipmentName, id: item.id }
        })
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.app-container {
  display: flex;
  width: 100%;
  max-width: 1600px;
  height: calc(100vh);
  overflow: hidden;
  box-sizing: border-box;
  background-color: #fafafa;
  .left-container {
    display: flex;
    flex: 3;
    flex-direction: column;
    margin: 0 15px;
    .equipment {
      display: flex;
      flex: 3;
      max-width: 500px;
      padding: 0 10px;
      // margin-left: 50px;
      margin-top: 20px;
      width: 100%;
      max-height: 280px;
      // box-sizing: border-box;
      box-shadow: 4px 0px 25px 6px rgba(156, 152, 152, 0.242);
      background-color: rgba(150, 215, 255, 0.242);
    }
    .answer-sheet {
      display: flex;
      width: 100%;
      margin-top: 30px;
      padding: 15px 5px 5px 20px;
      border-bottom: 1px solid #f5f5f5;
      overflow-y: auto;
      box-shadow: -4px 4px 15px 2px rgba(96, 95, 95, 0.32);
      background-color: rgba(150, 215, 255, 0.242);
      .question-type {
        display: flex;
        flex-direction: row;
        .type-title {
          font-weight: 700;
          margin: 15px 0px;
        }

        .el-tag {
          width: 35px;
          text-align: center;
          cursor: pointer;
        }
      }
      .select-topic {
        display: flex;
      }
      .question-type {
        .type-title {
          font-weight: 700;
          margin: 15px 0px;
        }

        .el-tag {
          width: 35px;
          text-align: center;
          margin: 0px 8px 8px 0px;
          padding: 0px;
          cursor: pointer;
        }
      }
    }
  }
  .right-container {
    display: flex;
    flex: 7;
    flex-direction: column;
    .question-list {
      height: calc(100vh - 175px);
      margin-left: 30px;
      overflow: auto;
      padding: 20px;
      box-sizing: border-box;
      scroll-behavior: smooth;
    }
  }

  .question {
    position: relative;
    width: 100%;
    background-color: #ffffff;
    padding: 15px 10px;
    margin-bottom: 10px;
    border-radius: 10px;
    box-shadow: rgba(0, 0, 0, 0.1) -4px 9px 25px -6px;
    background-color: rgba(150, 215, 255, 0.242);
  }

  ::v-deep .el-loading-mask {
    background-color: transparent;
  }
}
.noMore {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 900px;
  height: 100%;
  text-align: center;
  font-size: 18px;
  color: #606266;
  box-sizing: border-box;
}

.app-container {
  min-height: calc(100vh - 60px);
  box-sizing: border-box;
  // background: url('../../assets/shebei_images/BMD.jpg') center center no-repeat;
  width: 100%;
  background-size: 100% 100% !important;

  .tabs-container {
    display: flex;
    text-align: center;
    width: 100%;
    height: 300px;
    background-color: rgba(0, 0, 0, 0);
  }
}

.el-loading-mask {
  background-color: rgba(0, 0, 0, 0);
}
</style>
